package 回溯算法;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class 子集问题 {
    static class Solution{
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        public List<List<Integer>> subsets(int[] nums){
            if (nums.length == 0){
                result.add(new ArrayList<>());
                return result;
            }
            subsetHelper(nums,0);
            return result;

        }

        private void subsetHelper(int[] nums, int startIndex) {
            //遍历这个树，所有节点都记录下来
            //我觉得这里一开始执行这句就会把空集给算进来，就默认空集肯定是子集了
            result.add(new ArrayList<>(path));
            //终止条件可不加
            if (startIndex>=nums.length) return;
            for (int i = startIndex; i < nums.length; i++) {
                path.add(nums[i]);
                subsetHelper(nums,i+1);
                path.removeLast();
            }
        }
    }
}
